<?php require_once("../includes/functions.php"); ?>
<?php
	
class check	
{	

 	public $cashkFieldNames = array("row_num","amount","create_2","recived","manuy2client_manuy_number");								
					
 	public $cashFieldNamesPerManuy = array("row_num","amount","create_2","recived");								
	
        public function  viewCashPerManuy()
	{
		if ( isset($_POST['manuy2client_manuy_number']) )
                {
                    #----- view and filter sql query generator  ------#
                    $sql_query="SELECT * FROM cash WHERE ";
                    $sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
                    $sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                    foreach ($this->cashFieldNamesPerManuy as $fieldName) 
                    {
                            ${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
                            if ($fieldName === end($this->cashFieldNamesPerManuy))
                            $sql_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND manuy2client_manuy_number={$_POST['manuy2client_manuy_number']} {$sidx} {$sord};";
                            else	
                            $sql_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
                    }
                    //echo $sql_query;
                    #----- view and filter sql query generator  ------#

                    #----- get data from DB ------#
                    require("../includes/openConnection.php"); 
                    $q = mysql_query($sql_query) or die(mysql_error());
                    require("../includes/closeConnection.php");
                    #----- get data from DB ------#

                    #----- return json ------#
                    $rows = array();
                    while($r = mysql_fetch_assoc($q)) 
                            {	
                            //	$r['perosnid'] = $r['id'];
                            //  unset($r['id']);
                            $rows[] = $r;
                            }
                    print json_encode($rows);
                    #----- return json ------#
                }
	}
	
	public function  editCash()
	{
		
		

		
		#----- update sql query generator  ------#
		
		$key_field_name = reset($this->cashkFieldNames);
		$sql_query="UPDATE cash SET ";
		foreach ($this->cashkFieldNames as $fieldName) 
		{
			${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
			if ($fieldName === end($this->cashkFieldNames))
			$sql_query .= "{$fieldName} = '".${(string)$fieldName}."' WHERE $key_field_name =".${(string)$key_field_name}."; ";
			else	
			$sql_query .= "{$fieldName} = '".${(string)$fieldName}."', ";
		}
		//echo $sql_query;
		#----- update sql query generator  ------#
		
		#----- update data in DB ------#
		require("../includes/openConnection.php");
		if(mysql_query($sql_query)) echo "ok";
                 setAmlaDate($_POST ['manuy2client_manuy_number']);
		require("../includes/closeConnection.php");
		#----- update data in DB ------#
		
	}
	
	public function  addCash()
	{
            $today=date("Y-m-d");
		#----- insert data to DB ------#
		require("../includes/openConnection.php");  
                if(mysql_query("INSERT INTO cash (`row_num`, `amount`, `create_2`, `recived`, `manuy2client_manuy_number`) VALUES (NULL, '{$_POST ['amount']}', '{$today}', '{$_POST ['recived']}', '{$_POST ['manuy2client_manuy_number']}');")) echo "ok";
                setAmlaDate($_POST ['manuy2client_manuy_number']);
		$row_num=mysql_insert_id();
		require("../includes/closeConnection.php");
		#----- insert data to DB ------#	
		return $row_num;
	}
	
	public function  deleteCash()
	{
		#----- delete data to DB ------#
		
		require("../includes/openConnection.php");  
		if(mysql_query("DELETE FROM cash WHERE row_num='{$_POST ['row_num']}';")) echo "ok";
		require("../includes/closeConnection.php");
		#----- delete data to DB ------#	
			
	}

}




?>